import { createRouter, createWebHistory } from "vue-router";
const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: "/",
      name: "home",
      component: () => import("../views/login.vue"),
    },
    {
      path: "/index",
      name: "index",
      component: () => import("../views/admin/index.vue"),
      children: [
        {
          path: "/introduce",
          name: "introduce",
          meta: { title: "系统介绍" },
          component: () => import("../views/menu/Dashboard/introduce.vue"),
        },
        {
          path: "/dashboard",
          name: "dashboard",
          meta: { title: "大盘数据" },
          component: () => import("../views/menu/Dashboard/dashboard.vue"),
        },
        {
          path: "/add",
          name: "add",
          meta: { title: "添加商品" },
          component: () => import("../views/menu/Dashboard/add.vue"),
        },
        {
          path: "/swiper",
          name: "swiper",
          meta: { title: "轮播图配置" },
          component: () => import("../views/menu/HomeConfig/swiper.vue"),
        },
        {
          path: "/hot",
          name: "hot",
          meta: { title: "热销商品配置" },
          component: () => import("../views/menu/HomeConfig/hot.vue"),
        },
        {
          path: "/new",
          name: "new",
          meta: { title: "新品上线配置" },
          component: () => import("../views/menu/HomeConfig/new.vue"),
        },
        {
          path: "/recommend",
          name: "recommend",
          meta: { title: "为你推荐配置" },
          component: () => import("../views/menu/HomeConfig/recommend.vue"),
        },
        {
          path: "/category",
          name: "category",
          meta: { title: "分类管理" },
          component: () => import("../views/menu/ModuleConfig/category.vue"),
          children:[
            {
              path: "/category/level2",
              name: "level2",
              meta: { title: "分类二级管理" },
              component: () => import("../views/menu/ModuleConfig/level2.vue"),
            },
            {
              path: "/category/level3",
              name: "level3",
              meta: { title: "分类三级管理" },
              component: () => import("../views/menu/ModuleConfig/level3.vue"),
            }
          ]
        },
        {
          path: "/good",
          name: "good",
          meta: { title: "商品管理" },
          component: () => import("../views/menu/ModuleConfig/good.vue"),
        },
        {
          path: "/guest",
          name: "guest",
          meta: { title: "会员管理" },
          component: () => import("../views/menu/ModuleConfig/guest.vue"),
        },
        {
          path: "/order",
          name: "order",
          meta: { title: "订单管理" },
          component: () => import("../views/menu/ModuleConfig/order.vue"),
        },
        {
          path: "/account",
          name: "account",
          meta: { title: "修改账户" },
          component: () => import("../views/menu/SystemManagement/account.vue"),
        },
        {
          path: "/order_detail",
          name: "order_detail",
          meta: { title: "订单详情" },
          component: () => import("../views/menu/ModuleConfig/order_detail.vue"),
        },
      ],
    },
  ],
});
router.beforeEach((to, from, next) => {
  console.log(from);
  const token = localStorage.getItem("token");
  if (token) {
    if (to.path === "/") {
      next("/introduce");
    } else {
      next();
    }
  } else {
    if (to.path === "/") {
      next();
    } else {
      next("/");
    }
  }
});
export default router;
